package com.fingard.dsp.bank.directbank.jytpay01;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/** F.cruise
 *  金运通批量代收付
 */
public class JYTPAY01BatPayRec extends JYTPAY01Base implements IBatPayRec {
	/**
	 * 批量代付交易提交
	 * 		--  批量代付TC1004
	 *   	-- 用于批量代理商户将资金划转到企业或个人的银行账户。
	 *   	--  需要一次给多个银行账号付款的情况。
	 */
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988)	throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1988.ownAct, "");
			if(actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath) || !FileHelper.existFile(actItem.oppCertFilePath)) {
				retAts.respCode = "0";
				
				if(actItem == null){
					retAts.respInfo = "未找到账号"+p_req1988.ownAct+"的批量商户配置信息";
				}else{
					retAts.respInfo = "账号"+p_req1988.ownAct+"的证书文件不存在";
				}
				
				retAts.reqSeqID = p_req1988.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
				
				retAts.writeRetToATS();
				return retAts;
			}
			
			JYTPAY01Header bankHeader = new JYTPAY01Header();
			bankHeader.tran_code = "TC1004";
			bankHeader.tran_flowid = actItem.merchantId + Format.Date8Format.format(new Date()) + String.format("%06d", new Random().nextInt(999999));
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, bankHeader, actItem);
			
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("batch_num", p_req1988.totalNum);// 批次明细数  	最大值：500(金运通会自行判断并处理)
			xmlWriter.writeElementString("batch_amt", p_req1988.totalAmt);// 总金额
			
			// 批次处理类型 00：实时（交易窗口： 08:00~05:00）	01：非实时
			if (p_req1988.reqHeader.commandCode.equals("198801")) {
				xmlWriter.writeElementString("batch_type", "00");
			}else {
				xmlWriter.writeElementString("batch_type", "01");
			}
			
			xmlWriter.writeElementString("mer_viral_acct", "");
			xmlWriter.writeStartElement("list");
			
			int seq_id = 0;
			while(p_req1988.readRecord()) {
				seq_id++;
				
				xmlWriter.writeStartElement("detail");
				xmlWriter.writeElementString("seq_id", String.format("%04d", seq_id));// 组内序号
				xmlWriter.writeElementString("agrt_no", p_req1988.readedDetail.authAgNum);// 协议号
				xmlWriter.writeElementString("bank_name", p_req1988.readedDetail.recBankName);// 银行名称
				xmlWriter.writeElementString("account_no", p_req1988.readedDetail.recAct);// 银行账号
				xmlWriter.writeElementString("account_name", p_req1988.readedDetail.recName);// 银行账户名称
				
				// 账户类型 00：对私 01：对公
				if (p_req1988.readedDetail.isPrivate.equals("1")) {
					xmlWriter.writeElementString("account_type", "00");
				} else {
					xmlWriter.writeElementString("account_type", "01");
				}
				
				xmlWriter.writeElementString("brach_bank_province", p_req1988.readedDetail.getOppProvinceAndCity()[0]);// 省
				xmlWriter.writeElementString("brach_bank_city", p_req1988.readedDetail.getOppProvinceAndCity()[1]);// 市
				xmlWriter.writeElementString("brach_bank_name", "");// 开户行名称
				xmlWriter.writeElementString("tran_amt", p_req1988.readedDetail.payAmount);// 交易金额 保留两位小数
				xmlWriter.writeElementString("currency", convertCurToBank(p_req1988.readedDetail.recCur));
				xmlWriter.writeElementString("bsn_code", getBankConfig().getMapValueByKey("payUseMapping", "1"));// 代付业务类型
				xmlWriter.writeElementString("cert_type", "");
				xmlWriter.writeElementString("cert_no", "");
				xmlWriter.writeElementString("mobile", "");
				xmlWriter.writeElementString("remark", "");// 摘要
				xmlWriter.writeElementString("reserve", "");// 预留字段
				xmlWriter.writeEndElement();
			}
			
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			
			// 报文明文
			WriteBankLogLn2("金运通批量代付请求报文: \n" + xmlWriter.toXmlString());
			
			// 加密报文并生成签名
			String[] postStr = encryptAndSign(xmlWriter.toXmlString(),actItem);
			
			// http请求数据
			Map<String,String> postParams = new HashMap<String,String>();
			postParams.put("merchant_id", actItem.merchantId);
			postParams.put("xml_enc", postStr[0]);// 加密报文
			postParams.put("key_enc", postStr[1]);// 加密密钥
			postParams.put("sign", postStr[2]);// 签名
			
			// 发送金运通
			String[] tmpStrRet = sendToBank("unifiedPay",postParams);
			
			// 解析金运通返回
			if(tmpStrRet[0].length() == 0) {
				// http返回数据
				WriteBankLogLn2("金运通http返回数据: " + tmpStrRet[1]);
				
				String[] respHttpStr = tmpStrRet[1].split("&");
				String hexXmlEnc = respHttpStr[1].split("=")[1].replace("\"", "");// 报文密文
				String hexKeyEnc = respHttpStr[2].split("=")[1].replace("\"", "");// 密钥密文
				String hexSign = respHttpStr[3].split("=")[1].replace("\"", "");// 签名
				
				// 解密验签
				String[] tmpRespStr = decryptAndVeriSign(hexXmlEnc, hexKeyEnc, hexSign,actItem);
				String respXml = tmpRespStr[0];// 返回报文
				String respSign = tmpRespStr[1];// 验签结果
				
				// 验签处理
				if (respSign.equals("1")) {
					// 返回报文
					WriteBankLogStep3(respXml);
					
					// 解析报文
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(respXml, tmpSbMsg);
					
					if(tmpBkReader != null){
						String tmpRespCode = tmpBkReader.getFirstText("/message/head/resp_code");
						String tmpRespInfo = tmpBkReader.getFirstText("/message/head/resp_desc");
						
						if (tmpRespCode.equals("S0000000")) {
							retAts.respCode = "0";
							retAts.respInfo = tmpRespInfo;
							retAts.reqSeqID = p_req1988.reqBatchNO;
							retAts.transState = tmpRespCode + "+" + tmpBkReader.getFirstText("/message/body/batch_state");
							retAts.payInfoCode = tmpRespCode;
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpRespInfo;
						}
					}else{
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "验证签名失败!";
				}
			}else if(tmpStrRet[0].equals(ConnState.FAIL)){
				retAts.respCode = "0";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_req1988.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = tmpStrRet[0];
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_req1988.reqBatchNO;
		}
		
		retAts.writeRetToATS();
		return retAts;
	}

	/**
	 * 批量代付交易状态查询
	 * 		--  批量代付查询TC2004
	 * 		--	用于查询批量代付交易的处理结果
	 * 		--	批量代付提交后用于查询交易结果：正常情况下，批量交易的结果30分钟后返回
	 */
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998)	throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
		retAts.respCode = "";
		
		try{
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1998.ownAct, "");
			if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)|| !FileHelper.existFile(actItem.oppCertFilePath)) {
				if (actItem == null) {
					throw new Exception("未找到账号" + p_req1998.ownAct + "对应的商户配置节点");
				} else {
					throw new Exception("账号" + p_req1998.ownAct + "对应的证书文件不存在");
				}
			}
			
			JYTPAY01Header bankHeader = new JYTPAY01Header();
			bankHeader.tran_code = "TC2004";
			bankHeader.tran_flowid = actItem.merchantId + Format.Date8Format.format(new Date()) + String.format("%06d", new Random().nextInt(999999));
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, bankHeader, actItem);
			
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("orgTranFlow",p_req1998.reqBatchNO);// 商户原批次交易流水号
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			
			// 报文明文
			WriteBankLogLn2("金运通批量代付查询请求报文: \n" + xmlWriter.toXmlString());
			
			// 加密报文并生成签名
			String[] postStr = encryptAndSign(xmlWriter.toXmlString(),actItem);
			
			// http请求数据
			Map<String,String> postParams = new HashMap<String,String>();
			postParams.put("merchant_id", actItem.merchantId);
			postParams.put("xml_enc", postStr[0]);// 加密报文
			postParams.put("key_enc", postStr[1]);// 加密密钥
			postParams.put("sign", postStr[2]);// 签名
			
			// 发送金运通
			String[] tmpStrRet = sendToBank("unifiedPay",postParams);
						
			// 解析金运通返回
			if(tmpStrRet[0].length() == 0){
				// http返回数据
				WriteBankLogLn2("金运通http返回数据: \n" + tmpStrRet[1]);
				
				String[] respHttpStr = tmpStrRet[1].split("&");
				String hexXmlEnc = respHttpStr[1].split("=")[1].replace("\"", "");// 报文密文
				String hexKeyEnc = respHttpStr[2].split("=")[1].replace("\"", "");// 密钥密文
				String hexSign = respHttpStr[3].split("=")[1].replace("\"", "");// 签名
				
				// 解密验签
				String[] tmpRespStr = decryptAndVeriSign(hexXmlEnc, hexKeyEnc, hexSign,actItem);
				String respXml = tmpRespStr[0];// 返回报文明文
				String respSign = tmpRespStr[1];// 验签结果
				
				// 验签处理
				if (respSign.equals("1")) {
					// 返回报文
					WriteBankLogStep3(respXml);
					
					// 解析报文
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(respXml, tmpSbMsg);
				
					if(tmpBkReader != null) {
						String tmpRespCode = tmpBkReader.getFirstText("/message/head/resp_code");
						String tmpRespInfo = tmpBkReader.getFirstText("/message/head/resp_desc");
						
						if (tmpRespCode.equals("S0000000")) {
							retAts.respCode = "0";
							retAts.respInfo = tmpRespInfo;
							retAts.reqSeqID = p_req1998.reqBatchNO;
							retAts.transState = tmpRespCode + "+" + tmpBkReader.getFirstText("/message/body/batch_state");
							retAts.payInfoCode = tmpRespCode;
							retAts.writeBeforeDetail();
							
							while(tmpBkReader.read("/message/body/list/detail")){
								RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
								
								String tranRespCode = tmpBkReader.readedXElement.getSingleText("tran_resp_code");
						
								tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("account_no");
								tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("account_name");
								tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("tran_amt");
								tmpRetDetail.result = tranRespCode + "+" +tmpBkReader.readedXElement.getSingleText("tran_state");
								tmpRetDetail.payInfoCode = tranRespCode;
								
								retAts.writeRetDetail(tmpRetDetail);
							}
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpRespInfo;
						}
					}else{
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "验证签名失败!";
				}
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_req1998.reqBatchNO;
		}
		
		if(retAts.respCode.equals("-1")){
			retAts.writeBeforeDetail();
		}
		
		retAts.writeAfterDetail();
		return retAts;
	}

	/**
	 * 批量代扣交易提交
	 * 		--  批量代收 TC1003
	 * 		--  用于批量代理商户从个人的银行账户收款至虚拟账户
	 * 		--  需要一次给多个银行账号收款的情况
	 */
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req9188.ownAct, "");
			if(actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath) || !FileHelper.existFile(actItem.oppCertFilePath)){
				retAts.respCode = "0";
				
				if(actItem == null){
					retAts.respInfo = "未找到账号"+p_req9188.ownAct+"的批量商户配置节点";
				}else{
					retAts.respInfo = "账号"+p_req9188.ownAct+"的证书文件不存在";
				}
				
				retAts.reqSeqID = p_req9188.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
				
				retAts.writeRetToATS();
				return retAts;
			}
			
			JYTPAY01Header bankHeader = new JYTPAY01Header();
			bankHeader.tran_code = "TC1003";
			bankHeader.tran_flowid = actItem.merchantId + Format.Date8Format.format(new Date()) + String.format("%06d", new Random().nextInt(999999));
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, bankHeader, actItem);
			
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("batchTotalNum", p_req9188.totalNum);// 明细笔数不能超过500笔(金运通自行处理)
			xmlWriter.writeElementString("batchTotalAmt", p_req9188.totalAmt);
			xmlWriter.writeStartElement("list");
			
			int seq_id = 0;
			while(p_req9188.readRecord()) {
				seq_id++;
				
				xmlWriter.writeStartElement("detail");
				xmlWriter.writeElementString("seq_id", String.format("%04d", seq_id));// 批次组内序号
				xmlWriter.writeElementString("agrt_no", p_req9188.readedDetail.authAgNum);// 协议号
				xmlWriter.writeElementString("bank_name", p_req9188.readedDetail.payBankName);// 银行名称
				xmlWriter.writeElementString("account_no", p_req9188.readedDetail.payAct);// 银行账号
				xmlWriter.writeElementString("account_name", p_req9188.readedDetail.payName);// 银行账户名
				
				// 账户类型 00：对私
				if (p_req9188.readedDetail.isPrivate.equals("1")) {
					xmlWriter.writeElementString("account_type", "00");
				}
				
				xmlWriter.writeElementString("brach_bank_province", p_req9188.readedDetail.getOppProvinceAndCity()[0]);// 省
				xmlWriter.writeElementString("brach_bank_city", p_req9188.readedDetail.getOppProvinceAndCity()[1]);// 市
				xmlWriter.writeElementString("brach_bank_name", p_req9188.readedDetail.payBankLocation);// 开户行
				xmlWriter.writeElementString("tran_amt", p_req9188.readedDetail.payAmount);// 代付金额，保留两位小数
				xmlWriter.writeElementString("currency", convertCurToBank(p_req9188.readedDetail.payCur));// 付方币种
				xmlWriter.writeElementString("bsn_code", getBankConfig().getMapValueByKey("payUseMapping", "0"));// 代收业务类型
				xmlWriter.writeElementString("cert_type", convertCertTypeToBank(p_req9188.readedDetail.certType));// 证件类型
				xmlWriter.writeElementString("cert_no", p_req9188.readedDetail.certNum);// 证件号
				xmlWriter.writeElementString("mobile", "");// 预留手机号
				xmlWriter.writeElementString("remark", "");// 摘要
				xmlWriter.writeElementString("reserve", "");// 预留字段
				xmlWriter.writeEndElement();
			}
			
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			
			// 报文明文
			WriteBankLogLn2("金运通批量代扣交易请求报文: " + xmlWriter.toXmlString());
			
			// 加密报文并生成签名
			String[] postStr = encryptAndSign(xmlWriter.toXmlString(),actItem);
			
			// http请求数据
			Map<String,String> postParams = new HashMap<String,String>();
			postParams.put("merchant_id", actItem.merchantId);
			postParams.put("xml_enc", postStr[0]);// 报文密文
			postParams.put("key_enc", postStr[1]);// 会话密钥密文
			postParams.put("sign", postStr[2]);// 签名
			
			// 发送金运通
			String[] tmpStrRet = sendToBank("unifiedPay",postParams);
			
			//解析银行返回
			if(tmpStrRet[0].length() == 0){
				// http返回数据
				WriteBankLogLn2("金运通http返回数据: \n" + tmpStrRet[1]);
				
				String[] respHttpStr = tmpStrRet[1].split("&");
				String hexXmlEnc = respHttpStr[1].split("=")[1].replace("\"", "");// 报文密文
				String hexKeyEnc = respHttpStr[2].split("=")[1].replace("\"", "");// 会话密钥密文
				String hexSign = respHttpStr[3].split("=")[1].replace("\"", "");// 签名
				
				// 解密验签
				String[] tmpRespStr = decryptAndVeriSign(hexXmlEnc, hexKeyEnc, hexSign,actItem);
				String respXml = tmpRespStr[0];// 返回报文明文
				String respSign = tmpRespStr[1];// 验签结果
				
				// 验签处理
				if (respSign.equals("1")) {
					// 返回报文
					WriteBankLogStep3(respXml);
					
					// 解析报文
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(respXml, tmpSbMsg);
					
					if(tmpBkReader != null){
						String tmpRespCode = tmpBkReader.getFirstText("/message/head/resp_code");
						String tmpRespInfo = tmpBkReader.getFirstText("/message/head/resp_desc");
						
						if (tmpRespCode.equals("S0000000")) {
							retAts.respCode = "0";
							retAts.respInfo = tmpRespInfo;
							retAts.reqSeqID = p_req9188.reqBatchNO;
							retAts.transState = tmpRespCode + "+" + tmpBkReader.getFirstText("/message/body/batch_state");
							retAts.payInfoCode = tmpRespCode;
						}else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpRespInfo;
						}
					} else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "报文验证签名失败!";
				}
			}else if(tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)){
				retAts.respCode = "0";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_req9188.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = tmpStrRet[1];
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.toString();
			e.printStackTrace();
		}
		
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_req9188.reqBatchNO;
		}
		retAts.writeRetToATS();
		return retAts;
	}

	/**
	 * 批量代扣交易状态查询
	 * 		--  批量代收查询TC2003
	 * 		--	用于查询批量代收交易的处理结果。
	 * 		--  批量代收提交后用于查询交易结果。正常情况下，批量交易的结果30分钟后返回。
	 */
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req9198.reqHeader);
		retAts.respCode = "";
		
		try{
			ActSetItem actItem = getBankConfig().getActSetItem(p_req9198.ownAct, "");
			if(actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath) || !FileHelper.existFile(actItem.oppCertFilePath)){
				retAts.respCode = "0";
				
				if(actItem == null){
					retAts.respInfo = "未找到账号"+p_req9198.ownAct+"的批量商户配置节点";
				}else{
					retAts.respInfo = "账号"+p_req9198.ownAct+"的证书文件不存在";
				}
				
				retAts.reqSeqID = p_req9198.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
				
				return retAts;
			}
		
			JYTPAY01Header bankHeader = new JYTPAY01Header();
			bankHeader.tran_code = "TC2003";
			bankHeader.tran_flowid = actItem.merchantId + Format.Date8Format.format(new Date()) + String.format("%06d", new Random().nextInt(999999));
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, bankHeader, actItem);
			
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("orgTranFlow",p_req9198.reqBatchNO);// 商户原批次交易流水号
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			
			// 报文明文
			WriteBankLogLn2("金运通批量代扣状态查询请求报文: \n " + xmlWriter.toXmlString());
			
			// 加密报文并生成签名
			String[] postStr = encryptAndSign(xmlWriter.toXmlString(),actItem);
			
			// http请求数据
			Map<String,String> postParams = new HashMap<String,String>();
			postParams.put("merchant_id", actItem.merchantId);
			postParams.put("xml_enc", postStr[0]);// 报文密文
			postParams.put("key_enc", postStr[1]);// 加密后密钥
			postParams.put("sign", postStr[2]);// 签名
			
			// 发送金运通
			String[] tmpStrRet = sendToBank("unifiedPay",postParams);
			
			// 解析金运通http返回
			if(tmpStrRet[0].length() == 0){
				// http返回数据
				WriteBankLogLn2("金运通http返回数据: \n" + tmpStrRet[1]);
				
				String[] respHttpStr = tmpStrRet[1].split("&");
				String hexXmlEnc = respHttpStr[1].split("=")[1].replace("\"", "");// 报文密文
				String hexKeyEnc = respHttpStr[2].split("=")[1].replace("\"", "");// 密钥密文
				String hexSign = respHttpStr[3].split("=")[1].replace("\"", "");// 签名
				
				// 解密验签
				String[] tmpRespStr = decryptAndVeriSign(hexXmlEnc, hexKeyEnc, hexSign,actItem);
				String respXml = tmpRespStr[0];// 返回报文
				String respSign = tmpRespStr[1];// 验签结果
				
				// 验签处理
				if (respSign.equals("1")) {
					// 返回报文
					WriteBankLogStep3(respXml);
					
					// 解析报文
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(respXml, tmpSbMsg);
				
					if(tmpBkReader != null) {
						String tmpRespCode = tmpBkReader.getFirstText("/message/head/resp_code");
						String tmpRespInfo = tmpBkReader.getFirstText("/message/head/resp_desc");
						
						if (tmpRespCode.equals("S0000000")) {
							retAts.respCode = "0";
							retAts.respInfo = tmpRespInfo;
							retAts.reqSeqID = p_req9198.reqBatchNO;
							retAts.transState = tmpRespCode + "+" + tmpBkReader.getFirstText("/message/body/batchState");
							retAts.payInfoCode = tmpRespCode;
							retAts.writeBeforeDetail();
							
							while(tmpBkReader.read("/message/body/list/detail")){
								RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
								
								String tranRespCode = tmpBkReader.readedXElement.getSingleText("tran_resp_code");
								
								tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("account_no");
								tmpRetDetail.payName = tmpBkReader.readedXElement.getSingleText("account_name");
								tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("tran_amt");
								tmpRetDetail.result = tranRespCode + "+" +tmpBkReader.readedXElement.getSingleText("tran_state");
								tmpRetDetail.payInfoCode = tranRespCode;
						
								retAts.writeRetDetail(tmpRetDetail);
							}
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpRespInfo;
						}
					}else{
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "验证签名失败!";
				}
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_req9198.reqBatchNO;
		}
		
		if(retAts.respCode.equals("-1")){
			retAts.writeBeforeDetail();
		}
		
		retAts.writeAfterDetail();
		return retAts;
	}
}
